﻿using Microsoft.Xna.Framework;
using Terraria;
using Terraria.Audio;
using Terraria.GameContent.Bestiary;
using Terraria.ID;
using Terraria.ModLoader;

namespace HardcoreGame.Content.NPCs.Enemies.Events.FrostLegion
{
    public class ExplodingSnowman : ModNPC
    {
        public override void SetStaticDefaults()
        {
            Main.npcFrameCount[NPC.type] = 4;
        }
        public override void SetDefaults()
        {
            NPC.lifeMax = 240;
            NPC.damage = 50;
            NPC.defense = 20;
            NPC.knockBackResist = 0.3f;
            NPC.width = 42;
            NPC.aiStyle = 41;
            AIType = 144;
            NPC.height = 56;
            NPC.aiStyle = 38;
            NPC.npcSlots = 15f;
            NPC.HitSound = SoundID.NPCHit11;
            NPC.DeathSound = SoundID.NPCDeath15;
            NPC.value = Item.buyPrice(0, 0, 1, 21);
            AnimationType = 143;
            Music = MusicID.Boss3;
        }
        public override float SpawnChance(NPCSpawnInfo spawnInfo)
        {
            if (!NPC.AnyNPCs(144) || !Main.hardMode || spawnInfo.SpawnTileY >= Main.worldSurface)
            {
                return 0f;
            }
            return 0.08f;
        }
        public override void OnHitPlayer(Player target, Player.HurtInfo hurtInfo)
        {
            NPC.life = -1;
            HitEffect(new NPC.HitInfo()
            {
                Damage = hurtInfo.Damage,
                HitDirection = hurtInfo.HitDirection,
                Knockback = hurtInfo.Knockback,
            });
            NPC.checkDead();
        }
        public override void ModifyNPCLoot(NPCLoot npcLoot)
        {
            var DropRules = Main.ItemDropsDB.GetRulesForNPCID(NPCID.SnowBalla, false);
            foreach (var DropRule in DropRules)
            {
                npcLoot.Add(DropRule);
            }
        }
        public override void SetBestiary(BestiaryDatabase database, BestiaryEntry bestiaryEntry)
        {
            bestiaryEntry.Info.AddRange([
                BestiaryDatabaseNPCsPopulator.CommonTags.SpawnConditions.Invasions.FrostLegion,
                new FlavorTextBestiaryInfoElement("Mods.HardcoreGame.NPCs.ExplodingSnowman.Bestiary")
            ]);
        }
        public override void HitEffect(NPC.HitInfo hit)
        {
            if (NPC.life <= 0)
            {
                SoundEngine.PlaySound(SoundID.Item62, new Vector2(NPC.position.X, NPC.position.Y));
                NPC.position.X = NPC.position.X + NPC.width / 2;
                NPC.position.Y = NPC.position.Y + NPC.height / 2;
                NPC.width = 80;
                NPC.height = 80;
                NPC.position.X = NPC.position.X - NPC.width / 2;
                NPC.position.Y = NPC.position.Y - NPC.height / 2;
                for (int i = 0; i < 40; i++)
                {
                    int num629 = Dust.NewDust(new Vector2(NPC.position.X, NPC.position.Y), NPC.width, NPC.height, 31, 0f, 0f, 100, default, 2f);
                    Main.dust[num629].velocity *= 3f;
                    if (Main.rand.NextBool(2))
                    {
                        Main.dust[num629].scale = 0.5f;
                        Main.dust[num629].fadeIn = 1f + Main.rand.Next(10) * 0.1f;
                    }
                }
                for (int j = 0; j < 70; j++)
                {
                    int num630 = Dust.NewDust(new Vector2(NPC.position.X, NPC.position.Y), NPC.width, NPC.height, 6, 0f, 0f, 100, default, 3f);
                    Main.dust[num630].noGravity = true;
                    Main.dust[num630].velocity *= 5f;
                    num630 = Dust.NewDust(new Vector2(NPC.position.X, NPC.position.Y), NPC.width, NPC.height, 6, 0f, 0f, 100, default, 2f);
                    Main.dust[num630].velocity *= 2f;
                }
                for (int num631 = 0; num631 < 3; num631++)
                {
                    float scaleFactor10 = 0.33f;
                    if (num631 == 1)
                    {
                        scaleFactor10 = 0.66f;
                    }
                    if (num631 == 2)
                    {
                        scaleFactor10 = 1f;
                    }
                    Gore gore = Main.gore[Gore.NewGore(NPC.GetSource_Death(), new Vector2(NPC.position.X + NPC.width / 2 - 24f, NPC.position.Y + NPC.height / 2 - 24f), default, Main.rand.Next(61, 64), 1f)];
                    gore.velocity *= scaleFactor10;
                    gore.velocity.X++;
                    gore.velocity.Y++;
                    gore = Main.gore[Gore.NewGore(NPC.GetSource_Death(), new Vector2(NPC.position.X + NPC.width / 2 - 24f, NPC.position.Y + NPC.height / 2 - 24f), default, Main.rand.Next(61, 64), 2f)];
                    gore.velocity *= scaleFactor10;
                    gore.velocity.X--;
                    gore.velocity.Y++;
                    gore = Main.gore[Gore.NewGore(NPC.GetSource_Death(), new Vector2(NPC.position.X + NPC.width / 2 - 24f, NPC.position.Y + NPC.height / 2 - 24f), default, Main.rand.Next(61, 64), 1f)];
                    gore.velocity *= scaleFactor10;
                    gore.velocity.X++;
                    gore.velocity.Y--;
                    gore = Main.gore[Gore.NewGore(NPC.GetSource_Death(), new Vector2(NPC.position.X + NPC.width / 2 - 24f, NPC.position.Y + NPC.height / 2 - 24f), default, Main.rand.Next(61, 64), 1f)];
                    gore.velocity *= scaleFactor10;
                    gore.velocity.X--;
                    gore.velocity.Y--;
                }
                NPC.position.X = NPC.position.X + NPC.width / 2;
                NPC.position.Y = NPC.position.Y + NPC.height / 2;
                NPC.width = 10;
                NPC.height = 10;
                NPC.position.X = NPC.position.X - NPC.width / 2;
                NPC.position.Y = NPC.position.Y - NPC.height / 2;
            }
        }
    }
}
